package xycdao

import (
	"Cgo/front/models"
	"Cgo/global"
)

type xycAddressDao struct{}

var XycAddressDao = xycAddressDao{}

// 根据userId查询地址列表，根据id查询某个地址详情
func (xycAddressDao) FindAddressByUserIdDao(userId int, id int) ([]models.XycAddress, error) {
	var address []models.XycAddress
	if err := global.DB.Where(models.XycAddress{UserId: userId, Id: id}).Find(&address).Error; err != nil {
		return address, err
	}
	return address, nil
}

// 更新地址
func (xycAddressDao) UpdateAddressDao(address models.XycAddress) error {
	if err := global.DB.Save(&address).Error; err != nil {
		return err
	}
	return nil
}

// 删除地址
func (xycAddressDao) DeleteAddressDao(id int) error {
	if err := global.DB.Delete(&models.XycAddress{}, id).Error; err != nil {
		return err
	}
	return nil
}

// 新增地址
func (xycAddressDao) AddAddressDao(address models.XycAddress) error {
	if err := global.DB.Create(&address).Error; err != nil {
		return err
	}
	return nil
}
